import { readTextFromFile, saveText, uuid } from "@/utils";
import { DEFAULT_EMPTY_WORKFLOW_DATA } from "../flow";
import { ModelModeType } from "../types/app";
import { LogicalOperator } from "@/components/workflow/nodes/if-else/types";


export const fetchWorkflowDraft = async (file: string) => {

  let content = await readTextFromFile(file, {});
  if (content && content.length > 0) {
    try {
      return {
        data: JSON.parse(content),
        hash: uuid(),
      };
    } catch (error) {
      console.log(error);
    }
  }
  return {
    data: DEFAULT_EMPTY_WORKFLOW_DATA,
    hash: uuid(),
  };
}

export const syncWorkflowDraft = async ({ source, data }: { source: string; data: any }): Promise<{ hash: string, updated_at: number } | null> => {

  await saveText(source, JSON.stringify(data));

  return {
    hash: uuid(),
    updated_at: new Date().getUTCMilliseconds()
  }
}


export const fetchNodesDefaultConfigs = async () => {

  return [
    {
      "type": "llm",
      "config": {
        "prompt_templates": {
          "chat_model": {
            "prompts": [
              {
                "role": "system",
                "text": "You are a helpful AI assistant.",
                "edition_type": "basic"
              }
            ]
          },
          "completion_model": {
            "conversation_histories_role": {
              "user_prefix": "Human",
              "assistant_prefix": "Assistant"
            },
            "prompt": {
              "text": "Here are the chat histories between human and assistant, inside <histories></histories> XML tags.\n\n<histories>\n{{#histories#}}\n</histories>\n\n\nHuman: {{#sys.query#}}\n\nAssistant:",
              "edition_type": "basic"
            },
            "stop": [
              "Human:"
            ]
          }
        }
      }
    },
    {
      "type": "code",
      "config": {
        "variables": [
          {
            "variable": "arg1",
            "value_selector": []
          },
          {
            "variable": "arg2",
            "value_selector": []
          }
        ],
        "code_language": "python3",
        "code": "\ndef main(arg1: str, arg2: str) -> dict:\n    return {\n        \"result\": arg1 + arg2,\n    }\n",
        "outputs": {
          "result": {
            "type": "string",
            "children": null
          }
        }
      }
    },
    {
      "type": "template-transform",
      "config": {
        "variables": [
          {
            "variable": "arg1",
            "value_selector": []
          }
        ],
        "template": "{{ arg1 }}"
      }
    },
    {
      "type": "question-classifier",
      "config": {
        "instructions": ""
      }
    },
    {
      "type": "http-request",
      "config": {
        "method": "get",
        "authorization": {
          "type": "no-auth"
        },
        "body": {
          "type": "none"
        },
        "timeout": {
          "connect": 10,
          "read": 60,
          "write": 20,
          "max_connect_timeout": 10,
          "max_read_timeout": 60,
          "max_write_timeout": 20
        }
      }
    },
    {
      "type": "iteration",
      "config": {
        "is_parallel": false,
        "parallel_nums": 10,
        "error_handle_mode": "terminated"
      }
    },
    {
      "model": {
        "prompt_templates": {
          "completion_model": {
            "conversation_histories_role": {
              "user_prefix": "Human",
              "assistant_prefix": "Assistant"
            },
            "stop": [
              "Human:"
            ]
          }
        }
      }
    },
    {
      "type": "data-operator",
      "config": {
        "domains": [],
        "datasources": [],
        "method": "findone",
        "class_identifier": "",
        "timeout": {
          "connect": 10,
          "read": 60,
          "write": 20,
          "max_connect_timeout": 10,
          "max_read_timeout": 60,
          "max_write_timeout": 20
        }
      }
    },
    {
      "type": "meta-operator",
      "config": {
        "method": "get",
        "class_type": "class",
        "identifier": "",
        "outputs": {
          "result": {
            "type": "object",
            "children": null
          }
        },
        "timeout": {
          "connect": 10,
          "read": 60,
          "write": 20,
          "max_connect_timeout": 10,
          "max_read_timeout": 60,
          "max_write_timeout": 20
        }
      }
    },
    {
      "type": "sql-operator",
      "config": {
        "script": "",
        "is_query": true,
        "datasource": [],
        "outputs": {
          "result": {
            "type": "object",
            "children": null
          }
        },
      }
    },
    {
      "type": "webassembly",
      "config": {
        "method": "get",
        "class_type": "class",
        "identifier": "",
        "outputs": {
          "result": {
            "type": "object",
            "children": null
          }
        },
        "timeout": {
          "connect": 10,
          "read": 60,
          "write": 20,
          "max_connect_timeout": 10,
          "max_read_timeout": 60,
          "max_write_timeout": 20
        }
      }
    },
    {
      "type": "html-extractor",
      "config": {
        "url": "https://",
        "selector": "body",
        "outputs": {
          "result": {
            "type": "object",
            "children": null
          }
        },
        "timeout": {
          "connect": 10,
          "read": 60,
          "write": 20,
          "max_connect_timeout": 10,
          "max_read_timeout": 60,
          "max_write_timeout": 20
        }
      }
    },
    {
      "type": "cli",
      "config": {
        "variables": [
          {
            "variable": "arg1",
            "value_selector": []
          },
          {
            "variable": "arg2",
            "value_selector": []
          }
        ],
        "host": {},
        "command": "",
        "arguments": [],
        "outputs": {
          "result": {
            "type": "object",
            "children": null
          }
        },
      }
    },
    {
      "type": "speech-generator",
      "config": {
        "model": {

        },
        "prompt_speech": "",
        "prompt_text": "",
        "text": "",
        "options": {
        },
        "outputs": {
          "result": {
            "type": "object",
            "children": null
          }
        },
      }
    },
    {
      "type": "video-generator",
      "config": {
        "model": {

        },
        "prompt_speech": "",
        "prompt_text": "",
        "text": "",
        "options": {
        },
        "outputs": {
          "result": {
            "type": "object",
            "children": null
          }
        },
      }
    },
    {
      "type": "image-generator",
      "config": {
        "model": {
          provider: 'ollama',
          name: '',
          mode: ModelModeType.tts
        },
        "prompt_speech": "",
        "prompt_text": "",
        "text": "",
        "options": {
        },
        "outputs": {
          "result": {
            "type": "object",
            "children": null
          }
        },
      }
    },
    {
      "type": "wait",
      "config": {
        cases: {
          case_id: 'true',
          logical_operator: LogicalOperator.and,
          conditions: [],
        },
        timeout: 60000,
        interval: 10000,
      }
    },
  ]
}

export const fetchWorkflowRunHistory = (url) => {


  return {} as any
}

export const fetchChatRunHistory = (url) => {


  return {} as any
}

export const singleNodeRun = (appId: string, nodeId: string, params: object) => {


  return {} as any
}

export const getIterationSingleNodeRunUrl = (isChatFlow: boolean, appId: string, nodeId: string) => {


  return {} as any
}

export const publishWorkflow = (url: string) => {

  return {} as any
}

export const fetchPublishedWorkflow = (url) => {


  return {} as any
}

export const stopWorkflowRun = (url: string) => {


  return {} as any
}

export const fetchNodeDefault = (appId: string, blockType: any, query = {}) => {


  return {} as any
}

export const updateWorkflowDraftFromDSL = (appId: string, data: string) => {


  return {} as any
}

export const fetchCurrentValueOfConversationVariable = ({ url, params }) => {


  return {} as any
}
